package test;

import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.Date;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

/**
 * This class demonstrates the process of creating an index with Lucene 
 * for text files in a directory.
 */
public class TextFileIndexer {

	public static void main(String[] args) throws Exception{
		//This if for the 3.1!
		//fileDir is the directory that contains the text files to be indexed
		File fileDir  = new File("C:\\files_to_index ");
		File INDEX_DIR = new File("D:\\Program Files\\eclipse\\workspace\\cs6714\\files");

		//indexDir is the directory that hosts Lucene's index files
		File   indexDir = new File("C:\\luceneIndex");
		Analyzer a = new StandardAnalyzer(Version.LUCENE_36);
		IndexWriter writer = new IndexWriter(
				FSDirectory.open(INDEX_DIR), 
				new IndexWriterConfig(Version.LUCENE_36, a));
		File[] textFiles  = INDEX_DIR.listFiles();
		long startTime = new Date().getTime();

		//Add documents to the index
		for(int i = 0; i < textFiles.length; i++){
			if(textFiles[i].isFile() && textFiles[i].getName().endsWith(".txt")){
				System.out.println("File " + textFiles[i].getCanonicalPath() 
						+ " is being indexed");
				Reader fileReader = new FileReader(textFiles[i]);
				Document doc = new Document();
				/* document.add(Field.Text("content",textReader));
		       document.add(Field.Text("path",textFiles[i].getPath()));*/
				doc.add(new Field("content", fileReader));
				writer.addDocument(doc);
			}
		}

		//writer.optimize();
		writer.close();
		long endTime = new Date().getTime();

		System.out.println("It took " + (endTime - startTime) 
				+ " milliseconds to create an index for the files in the directory "
				+ fileDir.getPath());

		//This is for the 3.2
		//open the index and initialize a Searcher object
		Directory dir = FSDirectory.open(INDEX_DIR);
		IndexSearcher searcher = new IndexSearcher(IndexReader.open(dir));
		//create a QueryParser to parser the query string
		QueryParser parser = new QueryParser(Version.LUCENE_36, "content", a);
		//parse the query string to get a Query object, pass it to the 
		//searcher, and output the results
		String query = "";
		Query q = parser.parse(query);
		TopDocs tops = searcher.search(q, 10); // retrieve top-10 results
		for (int i = 0; i < tops.scoreDocs.length; i++) {
			System.out.println(tops.scoreDocs[i].doc); 
		}
	}
}
